package 创建线程;

public class 实现Runnable接口 {
    public static void main(String[] args) {
        //实现Runnable写法1
        Thread t1 = new Thread(new MyRunnable());
        t1.start();
        //实现Runnable写法2:匿名内部类
        Runnable r = new Runnable() {    //属于Runnable接口的实现类
            @Override
            public void run() {
                System.out.println("匿名内部类 run !");
            }
        };
        Thread t2 = new Thread(r);
        t2.start();
        //实现Runnable写法3:常用方法
        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("匿名内部类2 run !");
            }
        });
        t3.start();
        //实现Runnable写法4:λ表达式
        Thread t4 = new Thread(()-> System.out.println("匿名内部类3 run !"));
        t4.start();
    }
    private static class MyRunnable implements Runnable{
        @Override
        public void run() {
            System.out.println("自定义类 run !");
        }
    }
}
